#include "widget.h"
#include "ui_widget.h"
#include<QPainter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //使用定时器
    timer=new QTimer();

    //信号槽
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));

    resize(600,368);

    //初始化两条蛇的位置和大小
    QRect rect(300,180,nodeWidth,nodeHeight);
    snake.append(rect);
    addTop();
    addTop();
    QRect rect2(100, 180, nodeWidth, nodeHeight);
    snake2.append(rect2);
    addTop2();
    addTop2();


    //初始化小球
    addNewReward();
}
Widget::~Widget()
{
    delete ui;
}

//控制方向函数的定义
void Widget::keyPressEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_Up:
        if(moveFlag!=DIR_DOWN){
        moveFlag=DIR_UP;
        }
        break;
    case Qt::Key_Down:
        if(moveFlag!=DIR_UP){
        moveFlag=DIR_DOWN;
        }
        break;
    case Qt::Key_Right:
        if(moveFlag!=DIR_LEFT){
        moveFlag=DIR_RIGHT;
        }
        break;
    case Qt::Key_Left:
        if(moveFlag!=DIR_RIGHT){
        moveFlag=DIR_LEFT;
        }
        break;
        // wsad玩家的按键设置
            case Qt::Key_W:
                if(moveFlag2 != DIR_DOWN){
                    moveFlag2 = DIR_UP;
                }
                break;
            case Qt::Key_S:
                if(moveFlag2 != DIR_UP){
                    moveFlag2 = DIR_DOWN;
                }
                break;
            case Qt::Key_A:
                if(moveFlag2 != DIR_RIGHT){
                    moveFlag2 = DIR_LEFT;
                }
                break;
            case Qt::Key_D:
                if(moveFlag2 != DIR_LEFT){
                    moveFlag2 = DIR_RIGHT;
                }
                break;
     //空格用以暂停游戏
    case Qt::Key_Space:
        if(gameStart==false){
            gameStart=true;
            //启动定时器
            timer->start(time);
        }else{
            gameStart=false;
            timer->stop();
        }
        break;
    default:
        break;
    }
}

//蛇边长的函数定义
void Widget::growSnake(int direction) {
    switch (direction) {
      case DIR_UP:    addTop();    break;
      case DIR_DOWN:  addDown();   break;
      case DIR_RIGHT: addRight();  break;
      case DIR_LEFT:  addLeft();   break;
    }
}

void Widget::growSnake2(int direction) {
    switch (direction) {
      case DIR_UP:    addTop2();    break;
      case DIR_DOWN:  addDown2();   break;
      case DIR_RIGHT: addRight2();  break;
      case DIR_LEFT:  addLeft2();   break;
    }
}


void Widget::moveSnake(int direction) {
    switch (direction) {
      case DIR_UP:    addTop();    break;
      case DIR_DOWN:  addDown();   break;
      case DIR_RIGHT: addRight();  break;
      case DIR_LEFT:  addLeft();   break;
    }
    deleteLast();
}

void Widget::moveSnake2(int direction) {
    switch (direction) {
      case DIR_UP:    addTop2();    break;
      case DIR_DOWN:  addDown2();   break;
      case DIR_RIGHT: addRight2();  break;
      case DIR_LEFT:  addLeft2();   break;
    }
     deleteLast2();
}


void Widget::timeout() {    
    //蛇吃小球和运动的函数
    if (snake[0].intersects(rewardNode)) {
        addNewReward();
        growSnake(moveFlag);
    } else {        
        moveSnake(moveFlag);
    }
    // 检查第二条蛇是否吃到奖励
    if (snake2[0].intersects(rewardNode.toRect())) {
        addNewReward();
        growSnake2(moveFlag2);
    } else {      
        moveSnake2(moveFlag2);
    }
    update();  // 更新
}

//绘制函数

void Widget::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPen pen;
    QBrush brush;

    // 背景图片
    QPixmap pix;
    pix.load("D:/tmp/snake3.jpg");
    painter.drawPixmap(0,0,600,368,pix);

    // 绘制蛇
    drawSnake(painter, snake, Qt::darkMagenta);
    drawSnake(painter, snake2, Qt::green);

    // 画小球
    pen.setColor(Qt::red);
    brush.setColor(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rewardNode);

    if(checkContact()){
        QFont font("方正舒体",30,QFont::ExtraLight,false);
        painter.setFont(font);
        pen.setColor(Qt::black);
        painter.setPen(pen);
        QString resultText;
        switch(gameResult) {
            case PurpleWins: resultText = "紫色蛇赢了！"; break;
            case GreenWins: resultText = "绿色蛇赢了！"; break;
            case Draw: resultText = "平局！"; break;
            default: resultText = "GAME OVER!";
        }
        painter.drawText((this->width()-300)/2,
                         (this->height()-30)/2,
                         resultText);
        timer->stop();
    }

    QWidget::paintEvent(event);
}


void Widget::deleteLast(){
    snake.removeLast();
}

//两条蛇的运动add函数
void Widget::addTop(){
    QPointF leftTop;
    QPointF rightBotom;

    if(snake[0].y()-nodeHeight<0){
        leftTop =QPoint(snake[0].x(),this->height()-nodeHeight);
        rightBotom =QPoint(snake[0].x()+nodeWidth,this->height());
    }else{
        leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
        rightBotom =snake[0].topRight();
    }

    snake.insert(0 , QRectF(leftTop,rightBotom));
}
void Widget::addDown(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake[0].y()+nodeHeight*2>this->height()){
        leftTop = QPointF(snake[0].x(),0);
        rightBottom = QPointF(snake[0].x()+nodeWidth,nodeHeight);
       } else{
            leftTop=snake[0].bottomLeft();
            rightBottom=snake[0].bottomRight()+QPoint(0,nodeHeight);
        }
    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::addLeft(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake[0].x()-nodeWidth<0){
        leftTop =QPointF(this->width()-nodeWidth,snake[0].y());
        rightBottom = QPointF(snake[0].x()+width(),nodeHeight);
       } else{
            leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);
        }
     rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::addRight(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake[0].x()+nodeWidth>this->width()){
        leftTop =QPointF(0,snake[0].y());
        rightBottom = QPointF(snake[0].x()+width(),nodeHeight);
       } else{
            leftTop=snake[0].topRight();
        }
     rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
//增加新的小球的函数
void Widget::addNewReward(){
    rewardNode=QRectF(
                qrand()%(this->width()/20)*20,
                qrand()%(this->height()/20)*20,
                nodeWidth,
                nodeHeight);
}

//为第二条蛇准备的函数
void Widget::deleteLast2(){
    snake2.removeLast();
}
void Widget::addTop2(){
    QPointF leftTop;
    QPointF rightBotom;

    if(snake2[0].y()-nodeHeight<0){
        leftTop =QPoint(snake2[0].x(),this->height()-nodeHeight);
        rightBotom =QPoint(snake2[0].x()+nodeWidth,this->height());
    }else{
        leftTop=QPointF(snake2[0].x(),snake2[0].y()-nodeHeight);
        rightBotom =snake2[0].topRight();
    }

    snake2.insert(0 , QRectF(leftTop,rightBotom));
}
void Widget::addDown2(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake2[0].y()+nodeHeight*2>this->height()){
        leftTop = QPointF(snake2[0].x(),0);
        rightBottom = QPointF(snake2[0].x()+nodeWidth,nodeHeight);
       } else{
            leftTop=snake2[0].bottomLeft();
            rightBottom=snake2[0].bottomRight()+QPoint(0,nodeHeight);
        }
    snake2.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::addLeft2(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake2[0].x()-nodeWidth<0){
        leftTop =QPointF(this->width()-nodeWidth,snake2[0].y());
        rightBottom = QPointF(snake2[0].x()+width(),nodeHeight);
       } else{
            leftTop=snake2[0].topLeft()-QPointF(nodeWidth,0);
        }
     rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake2.insert(0,QRectF(leftTop,rightBottom));
}
void Widget::addRight2(){
    QPointF leftTop;
    QPointF rightBottom;
    if(snake2[0].x()+nodeWidth>this->width()){
        leftTop =QPointF(0,snake2[0].y());
        rightBottom = QPointF(snake2[0].x()+width(),nodeHeight);
       } else{
            leftTop=snake2[0].topRight();
        }
     rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake2.insert(0,QRectF(leftTop,rightBottom));
}
//绘制蛇的函数
void Widget::drawSnake(QPainter &painter, const QList<QRectF> &snake, QColor color) {
    QPen pen(Qt::black);
    QBrush brush(color);
    painter.setPen(pen);
    painter.setBrush(brush);
    for (int i = 0; i < snake.length(); i++) {
        painter.drawRect(snake[i]);
    }
}


